package me.zhengjie.utils;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.ItemOrderInfo;
import com.alipay.api.domain.OrderExtInfo;
import com.alipay.api.domain.OrderLogisticsInformationRequest;
import com.alipay.api.domain.AlipayMerchantOrderSyncModel;
import com.alipay.api.response.AlipayMerchantOrderSyncResponse;
import com.alipay.api.request.AlipayMerchantOrderSyncRequest;

import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class AlipayMerchantOrderSync {

    public static void main(String[] args) throws AlipayApiException, ParseException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        // 构造请求参数以调用接口
        AlipayMerchantOrderSyncRequest request = new AlipayMerchantOrderSyncRequest();
        AlipayMerchantOrderSyncModel model = new AlipayMerchantOrderSyncModel();

        // 设置订单金额
        model.setAmount("9.9");

        // 设置订单创建时间
        model.setOrderCreateTime(sdf.parse("2025-04-01 08:00:00"));

        // 设置订单修改时间
        model.setOrderModifiedTime(sdf.parse("2025-04-01 08:10:01"));

        // 设置优惠金额
        model.setDiscountAmount("0");

        // 设置支付金额
        model.setPayAmount("9.9");

        // 设置商品信息列表
        List<ItemOrderInfo> itemOrderList = new ArrayList<ItemOrderInfo>();
        ItemOrderInfo itemOrderList0 = new ItemOrderInfo();
        itemOrderList0.setItemName("苹果");
        itemOrderList0.setQuantity(20L);
        itemOrderList0.setUnitPrice("13.18");



        List<OrderExtInfo> extInfovJRSC = new ArrayList<OrderExtInfo>();

        OrderExtInfo extInfodlcVW0 = new OrderExtInfo();
        extInfodlcVW0.setExtKey("image_material_id");
        extInfodlcVW0.setExtValue("2025041000502200000068583863");
        extInfovJRSC.add(extInfodlcVW0);

        OrderExtInfo extInfovJRSC0 = new OrderExtInfo();
        extInfovJRSC0.setExtKey("MY_KEY");
        extInfovJRSC0.setExtValue("MY_VALUE");
        extInfovJRSC.add(extInfovJRSC0);
        itemOrderList0.setExtInfo(extInfovJRSC);
        itemOrderList.add(itemOrderList0);
        model.setItemOrderList(itemOrderList);

        // 设置外部订单号
        model.setOutBizNo("123452");

        // 设置扩展信息
        List<OrderExtInfo> extInfordxbu = new ArrayList<OrderExtInfo>();
        OrderExtInfo extInfordxbu0 = new OrderExtInfo();
        extInfordxbu0.setExtKey("business_info");
        extInfordxbu0.setExtValue("{\"refund_amount\":\"30元\",\"invoice\":\"https://open.alipay.com:33333/app/pdf/proUrl\",\"rebate_pid\":\"20880000000000000\",\"come_again\":\"alipays://platformapi/startapp?appId=2014072300007148page=pages%2Ftakefood%2Findexquery=multi_id%3D25977\"}");
        extInfordxbu.add(extInfordxbu0);
        OrderExtInfo extInfordxbu1 = new OrderExtInfo();
        extInfordxbu1.setExtKey("merchant_biz_type");
        extInfordxbu1.setExtValue("KX_SHOPPING");
        extInfordxbu.add(extInfordxbu1);
        OrderExtInfo extInfordxbu2 = new OrderExtInfo();
        extInfordxbu2.setExtKey("merchant_order_status");
        extInfordxbu2.setExtValue("WAIT_PAY");
        extInfordxbu.add(extInfordxbu2);
        OrderExtInfo extInfordxbu3 = new OrderExtInfo();
        extInfordxbu3.setExtKey("merchant_order_link_page");
        extInfordxbu3.setExtValue("/pages/order/orderDetail/orderDetail?orderId=2021083100000000");
        extInfordxbu.add(extInfordxbu3);
        OrderExtInfo extInfordxbu4 = new OrderExtInfo();
        extInfordxbu4.setExtKey("tiny_app_id");
        extInfordxbu4.setExtValue("2021004128699319");
        extInfordxbu.add(extInfordxbu4);
        model.setExtInfo(extInfordxbu);

        // uid参数未来计划废弃，存量商户可继续使用，新商户请使用openid。请根据应用-开发配置-openid配置选择支持的字段。
         model.setBuyerId("2088112162998728");

        // 设置订单来源
        model.setSourceApp("Alipay");

        // 设置订单类型
        model.setOrderType("SERVICE_ORDER");

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayMerchantOrderSyncResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCsZjaKJA5H9iBmfRze/bHWnj94PO0sKeIyZpWyjXsyxcsQCsp7c5/CW7ygoQrBsf8h4DQxFM/6CRndjMgIE9fUEUDKEp5hd2Xezi573Zcdi/ObYNHF9SOybdn8N//wggZLDQdSWVeXQaQjWE6/ehcA3wlKx9TqsAKtydzNc6fejtjtHm4abwsWikXooPIKqZdZEq5Cl8F+YBKrBAbfrs0HOzBqlomt65rjvHO+CZl/wIn4S5ZW0IRWRLZ3Bi21v1treP47atl/L3Uh3Kt01kynYr9dqXQoiceQZD2nWMD1Ky8zy/Ix0WnHhu2hhyXDw43RhCCZ0u2f/+XOqEkT7JRbAgMBAAECggEBAIoXaZJnZSURKkzN5KVdkui1Y2OdceDRP8VdVIVsPIWj147nBd+y8wIM11/W/zW9rP5mh4fVAu5mAdjr3oJR2qyckUgLd9/ccrXwc112VogtRRltiTpGrKxMv7H+7Typ+f591iKJO9cFlkNVheZsFib1gIzs2kiOaebo7buH66wha51QmTOLm848ygb5utQhl+JnqO5u+fpyFo2jbPufT0W/fsxbWuJhBBTV9oBpsk7MlpZ3G10arnqMBrYzgkJyigtX7FlK8+5HKim8pweUhAxsO8E372R4PjL/ZYdLfE7+zBjFha2h/m34jLDECfi12I16U9L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OvR+39UWln2ZWCt3wnNpTqjA97ujnZXIl2jLnNA7QoyePY3sGLi7+G7aRy68wLTHMlAyc5FdO+RtmSJ7ZGyaAwXteG44AIJY0Zgg5pQ+04jSOarxstyb4GmINp77sgTon+Gf7iQMFRgHP6KKxD//VDxOX8cPERm9u24YCI+dZkNWnmAXbPuB8KUoCanYgHp/EJSoH2jETZ4u7ADWv5wYtXJQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021004128699319");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}
